package com.platform.comm.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Objects;

/**
 * Description:
 *
 * @author zyk
 */
public class FeignAuthInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        RequestContextHolder.setRequestAttributes(requestAttributes,true);
        if (!Objects.isNull(requestAttributes)) {
            String payload = ((ServletRequestAttributes) requestAttributes).getRequest().getHeader("payload");
            if(StringUtils.isNotBlank(payload)){
                template.header("payload",payload);
            }
            String authorization = requestAttributes.getRequest().getHeader("Authorization");
            if(StringUtils.isNotBlank(authorization)){
                template.header("Authorization",authorization);
            }

        }

    }
}
